$(function () {
    getGradeList();
    var beginDate = getWeekTime(0);
    var endDate = getWeekTime(-6);;
    $("#startEnd").val(beginDate+" 至 "+endDate);
    allPlanningTable();
})

// 获取时间
function getWeekTime(n){
    var now=new Date();
    var year=now.getFullYear();
//因为月份是从0开始的,所以获取这个月的月份数要加1才行
    var month=now.getMonth()+1;
    var date=now.getDate();
    var day=now.getDay();
//判断是否为周日,如果不是的话,就让今天的day-1(例如星期二就是2-1)
    if(day!==0){
        n=n+(day-1);
    }
    else{
        n=n+day;
    }
    if(day){
//这个判断是为了解决跨年的问题
        if(month>1){
            month=month;
        }
//这个判断是为了解决跨年的问题,月份是从0开始的
        else{
            year=year-1;
            month=12;
        }
    }
    now.setDate(now.getDate()-n);
    year=now.getFullYear();
    month=now.getMonth()+1;
    date=now.getDate();
    s=year+"-"+(month<10?('0'+month):month)+"-"+(date<10?('0'+date):date);
    return s;
}



/*获取年级下拉数据*/
function getGradeList() {
    $.DemoPost("/classInfo/getClassListByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#classList").append(" <option value='"+d.id+"'>"+d.gradeName+""+d.className+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}


function query(params) {
    var startEnd = $("#startEnd").val();
    var weekNum = $("#weekNum").val();
    var starTime = "";
    var endTime = "";
    if(startEnd!='')
    {
        var timeStr = startEnd.split("至");
        starTime = timeStr[0].replace(/^\s+|\s+$/g,"");
        endTime = timeStr[1].replace(/^\s+|\s+$/g,"");
    }
    var param = {
        weekNum:weekNum,
        beginDate:starTime,
        endDate:endTime,
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

function allPlanningTable() {
    $("#planning_list").bootstrapTable({
        url:$.ip+"/teacherplanning/getTeacherPlanningByUserId?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '班级',
                field: 'ban',
                align: 'center',
                valign: 'middle',
                formatter:function (id,row) {
                    var gradeName = row.gradeName;
                    var className = row.className;
                    return gradeName+""+className;
                }
            },
            {
                title: '教师名称',
                align: 'center',
                field: 'teacherName',
                valign: 'middle',

            } ,
            {
                title: '日期',
                align: 'center',
                field: 'planningDate',
                valign: 'middle',

            }  ,
            {
                title: '星期',
                align: 'center',
                field: 'weekName',
                valign: 'middle',

            }  ,
            {
                title: '时间',
                align: 'center',
                field: 'planningTime',
                valign: 'middle',

            }  ,
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="removeTr(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a> &nbsp<a data-id="'+id+'" onclick="update(this)" class="btn btn-primary " href="javascript:;">修改</a>';
                }
            }
        ]
    });
}


function removeTr(obj){
    tan("是否确认删除？",function (index,layero) {
        var id = $(obj).data("id");
        $.DemoGet("/teacherplanning/delete/"+id,
            function (data) {
                if(data.success)
                {
                    $("#planning_list").bootstrapTable('refresh');
                    layer.msg("删除成功");
                }
                else {
                    layer.msg(data.msg);
                }
            },function (e) {
                layer.msg("删除失败");
            });
    });
}


function add(){
    $("#weekNum_lab").val("");
    $("#time").val("");
    $("#courseDes").val("");
    $("#classList").val("");
    popdiv("class-info","添加",["50%","50%"],function (index,layero) {
        var weekNum = $("#weekNum_lab").val();
        var classId = $("#classList").val();
        var courseDate = $("#time").val();
        var courseDes = $("#courseDes").val();
        if(classId=='')
        {
            layer.msg("请选择班级");
        }
        else if(courseDate=='')
        {
            layer.msg("请选择排课时间");
        }
        else if(weekNum=='')
        {
            layer.msg("请选择星期");
        }
        else {
            $.DemoPost("/teacherplanning/managerAdd?token="+sessionStorage.getItem("token"),
                {"weekNum":weekNum,"classId":classId,"courseDate":new Date(courseDate),"courseDes":courseDes},
                function (data) {
                    if(data.success)
                    {
                        layer.close(index);
                        $("#planning_list").bootstrapTable('refresh');
                    }
                    layer.msg(data.msg);
                },function (e) {
                    layer.msg("添加失败");
                });

        }

    })
}
function update(obj){
    var id = $(obj).data("id");
    $.DemoGet("/teacherplanning/info/"+id,
        function (data) {
            if(data.success)
            {
                $("#id").val(data.obj.id);
                $("#weekNum_lab").val(data.obj.weekNum);
                $("#time").val(data.obj.planningDate);
                $("#courseDes").val(data.obj.courseDes);
                $("#classList").val(data.obj.classId);
                popdiv("class-info","编辑",["50%","50%"],function (index,layero) {
                    var weekNum = $("#weekNum_lab").val();
                    var classId = $("#classList").val();
                    var courseDate = $("#time").val();
                    var courseDes = $("#courseDes").val();
                    var id =  $("#id").val();
                    if(classId=='')
                    {
                        layer.msg("请选择班级");
                    }
                    else if(courseDate=='')
                    {
                        layer.msg("请选择排课时间");
                    }
                    else if(weekNum=='')
                    {
                        layer.msg("请选择星期");
                    }
                    else {
                        $.DemoPost("/teacherplanning/managerUpdate",
                            {"id":id,"weekNum":weekNum,"classId":classId,"courseDate":new Date(courseDate),"courseDes":courseDes},
                            function (data) {
                                if(data.success)
                                {
                                    layer.close(index);
                                    $("#planning_list").bootstrapTable('refresh');
                                }
                                layer.msg(data.msg);
                            },function (e) {
                                layer.msg("修改失败");
                            });

                    }
                })
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("获取详情信息失败");
        });

}
function getweek(){

    lookInfo("week-class","本周课程",["50%","50%"]);
}
//时间
laydate.render({
    elem: '#time',
    type: 'datetime'
    ,done: function(value, date, endDate){//日期时间被切换后的回调
        if(date['hours']<1)
        {
            layer.msg("还未选择小时分");
        }
        else {
            if(value!=''){
                var date1 = new Date(value);
                var week = date1.getDay();
                $("#weekNum_lab").val(week);
            }
            else {
                $("#weekNum_lab").val("");
            }
        }
    }
});
laydate.render({
    elem: '#startEnd'
    ,format: 'yyyy-MM-dd'
    ,range: '至'
});

$("#btnQuery").click(function () {
    $("#planning_list").bootstrapTable('refresh');
});